<?php
/** .-------------------------------------------------------------------
 * |  Software: [HDCMS framework]
 * |      Site: www.hdcms.com
 * |-------------------------------------------------------------------
 * |    Author: 向军 <2300071698@qq.com>
 * |    WeChat: aihoudun
 * | Copyright (c) 2012-2019, www.houdunwang.com. All Rights Reserved.
 * '-------------------------------------------------------------------*/

namespace web\home\controller;
/**
 * 详情控制器
 * 2016年10月24日19:52:54
 * 刘超群
 */


class Item{
    //动作
	protected $db;
	public function __construct()
	{
		$this->db = new \system\model\Goods();
	}
	public function index()
	{

		//接受商品id
		//hover数据
		$proData = Db::table('goods')->where('cid',8)->limit(3)->get();
		View::with('proData',$proData);
		$blueData = Db::table('goods')->where('cid',9)->limit(3)->get();
//        p($blueData);
		View::with('blueData',$blueData);
		$gid = q('get.gid',0,'intval');
//		$gids = $this->db->lists('gid');
//		if(!in_array($gid,$gids)){go('Entry/index');}
		//在类型属性表获取规格
		$tid = $this->db->where('gid',$gid)->pluck('tid');
		$specDate = Db::table('typeattr')->where('class',1)->where('tid',$tid)->get();

		foreach($specDate as $k=>$v)
		{
			$specDate[$k]['tavalue'] = Db::table('goods_attr')
				->where('taid',$v['taid'])->where('gid',$gid)->get();
		}
//		p($specDate);
		$specData = Db::table('typeattr')->where('class',2)->where('tid',$tid)->get();
		foreach($specData as $k=>$v)
		{
			$specData[$k]['tavalue'] = Db::table('goods_attr')
				->where('taid',$v['taid'])->where('gid',$gid)->get();
		}

//		p($specData);
		$goodsDetailModel = Db::table('detail')->where('gid',$gid)->first();
//		p($goodsDetailModel);
		$goodsDetailModel['medium'] = explode(',',$goodsDetailModel['medium']);
		$goodsDetailModel['big'] = explode(',',$goodsDetailModel['big']);
		$goodsData =Db::table('goods')->where('gid',$gid)->first();
//		p($goodsData);
		$_SESSION['home']['userurl'] = $_SERVER['REQUEST_URI'];
		View::with('goodsData',$goodsData);
//		p($goodsDetailModel);
		View::with('goodsDetailModel',$goodsDetailModel);
		View::with('specData',$specData);
		View::with('specDate',$specDate);

		View::make();
	}
	public function ajaxGetTotal()
	{
		$data = $_POST;
//        Array
//        (
//            [gid] => 10
//            [combine] => 165,167,
//        )
		//根据组合id和商品id在货品表查数据
		$data['combine'] = rtrim($data['combine'],',');
		$data = Db::table('goods_list')->where('combine',$data['combine'])->where('gid',$data['gid'])->first();
		//如果找不到数据
		if(!$data){echo json_encode(['valid'=>0]);exit;}
		//如果或存为0
		if($data['inventory']==0){echo json_encode(['valid'=>0]);exit;}
		//能走到这里，说明有数据存在并且货存不为0
		echo json_encode(['valid'=>1,'total'=>$data['inventory']]);exit;
	}
	public function ajaxCollect(){
		$data = $_POST;
		if ($_SESSION['admin']['logid']){
			Db::table('collect')->insert(['uid'=>$_SESSION['admin']['logid'],'gid'=>$data['gid']]);
			echo json_encode(['val'=>1]);exit;
		}else{
			echo json_encode(['val'=>0]);exit;
		}
	}
	public function ajaxAddCart()
	{
		if ($_SESSION['admin']['logid']){
			unset($_SESSION['cart']);
			$data = $_POST;
//        p($data);die;
			//获取商品单一数据
			$onGoodsData = Db::table('goods')->where('gid',$data['gid'])->first();
			$data = [
				'id'        => $data['gid'], // 商品 ID
				'name'      =>$onGoodsData['gname'],// 商品名称
				'num'       => $data['num'], // 商品数量
				'price'     => $onGoodsData['shopprice'], // 商品价格
				'options'   => $data['options'],// 其他参数如价格、颜色、可以为数组或字符串
			];
			Cart::add($data);
//            p($_SESSION);die;
			echo json_encode(['val'=>1]);exit;
		}else{
			echo json_encode(['val'=>0]);exit;
		}

	}
}
